﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Project_ScansiaPacific
{
    public partial class FrMayBao : Form
    {
        public FrMayBao()
        {
            InitializeComponent();
        }
        private string string_temp, MaSP1,Serial1, MaCT1, child = " - ", TenSP1,TenCT1,LoaiGo1,GhiChu1,Ca1,CDay1,CRong1,CDai1,TenMay1,DateBegin1,DateEnd1,SLDat1,SLTramTret1,SLLoai1;
        private int i = 0;
        private double SLDat_d=0, SLLoai_d=0, SLTramTret_d=0;
        private Process prs = new Process();
        private void cb_MaSP_input_SelectedIndexChanged(object sender, EventArgs e)
        {
            Process prs = new Process();
            //FormProcess Frp = new FormProcess();
            MaSP1 = prs.substring_right(cb_MaSP_input.Text, child);
            cb_MaCT_input.Items.Clear();
            cb_MaCT_input.Text = "";
            //string sql_load = @"select MaChiTiet,TenChiTiet from dbo.Load_CTSP_Mua inner join dbo.ChiTiet_KhuVuc on ChiTiet_KhuVuc.MaCT=dbo.Load_CTSP_Mua.MaChiTiet and ChiTiet_KhuVuc.KhuVuc='KV1N' where dbo.Load_CTSP_Mua.MaSP='" + MaSP1 + "'";
            // MessageBox.Show(dataGridView_LoadSP.Rows[1].Cells[0].Value.ToString());
            //Frp.cb1(cb_MaCTinput, sql_load, 1, 0);
            for (i = dataGridView_LoadSP.Rows.Count - 2; i >= 0; i--)
            {
                if (MaSP1 == dataGridView_LoadSP.Rows[i].Cells[0].Value.ToString())
                {
                    cb_MaCT_input.Items.Add(dataGridView_LoadSP.Rows[i].Cells[1].Value.ToString());
                }
            }
        }

        private void cb_MaCT_input_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_MaCT_input.Text != "")
            {
                for (i = 0; i < dataGridView_LoadSP.Rows.Count - 1; i++)
                {
                    if (cb_MaCT_input.Text == dataGridView_LoadSP.Rows[i].Cells[1].Value.ToString())
                    {
                        txt_CDay_input.Text = dataGridView_LoadSP.Rows[i].Cells["ChieuDaySC"].Value.ToString();
                        txt_CRong_input.Text = dataGridView_LoadSP.Rows[i].Cells["ChieuRongSC"].Value.ToString();
                        txt_CDai_input.Text = dataGridView_LoadSP.Rows[i].Cells["ChieuDaiSC"].Value.ToString();
                       
                        break;                        
                    }
                }
                
            }
        }

        private void FrMayBao_Load(object sender, EventArgs e)
        {
            Data dta = new Data();
            //FormProcess Frp = new FormProcess();
            SqlConnection Conn = dta.Connect();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Conn;
            Conn.Open();


            try
            {
                //cmd.CommandText = "select convert(varchar(10),getdate(),101)";
                cmd.CommandText = "select MaSP,TenSP from dbo.Load_cbSP";
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    cb_MaSP_input.Items.Add(rd[1].ToString() + " - " + rd[0].ToString());
                }
               // MessageBox.Show(rd[0].ToString());
                rd.Close();
                //string sql_load = @"select * from dbo.Load_cbSP";
                // Frp.cb1(cb_MaSP_input, string_temp, 1, 0);
                DataSet ds = new DataSet();
                string_temp = @"select dbo.Load_CTSP_Mua.MaSP,dbo.Load_CTSP_Mua.TenChiTiet+' - '+dbo.Load_CTSP_Mua.MaChiTiet,Load_CTSP_Mua.ChieuDaySC,ChieuRongSC,ChieuDaiSC from dbo.Load_CTSP_Mua";
                ds = dta.dts(string_temp);
                dataGridView_LoadSP.DataSource = ds.Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        private void reset()
        {
            cb_MaSP_input.Text = "";
            cb_MaCT_input.Text = "";
            txt_CDai_input.Text = "";
            txt_CRong_input.Text = "";
            txt_CDay_input.Text = "";
            txt_Dat_input.Text = "";
            txt_Loai_input.Text = "";
            txt_Tramtret_input.Text = "";
            cb_MaSP_input.Focus();
            if (dataGridViewX1.Rows.Count > 1)
                dataGridViewX1.Rows.Clear();
            cb_May_input.Enabled = true;
        }
        private void Add_grid()
        {
            try
            {
                Process prs = new Process();
                if (cb_Ca_input.Text == "" || cb_MaCT_input.Text == "" || cb_MaSP_input.Text == "" || cb_May_input.Text == "" || txt_CDai_input.Text == "" || txt_CDay_input.Text == "" || txt_CRong_input.Text == "")
                    MessageBox.Show("Bạn đang nhập thiếu thông tin");
                else
                {
                    TenSP1 = prs.substring_left(cb_MaSP_input.Text, child);
                   
                    MaCT1 = prs.substring_right(cb_MaCT_input.Text, child);
                    TenCT1 = prs.substring_left(cb_MaCT_input.Text, child);
                   
                    Ca1 = cb_Ca_input.Text.Trim();
                    LoaiGo1 = cb_LoaiGo_input.Text.Trim();
                    GhiChu1 = txt_GhiChu_input.Text.Trim();
                    CDay1 = txt_CDay_input.Text.Trim();
                    CRong1 = txt_CRong_input.Text.Trim();
                    CDai1 = txt_CDai_input.Text.Trim();
                    //MessageBox.Show("aaaa");
                    if (txt_Dat_input.Text == "")
                        SLDat_d = 0;
                    else
                        SLDat_d = double.Parse(txt_Dat_input.Text);
                    if (txt_Tramtret_input.Text == "")
                        SLTramTret_d = 0;
                    else
                        SLTramTret_d = double.Parse(txt_Tramtret_input.Text);
                    if (txt_Loai_input.Text == "")
                        SLLoai_d = 0;
                    else
                        SLLoai_d = double.Parse(txt_Loai_input.Text);
                    //MessageBox.Show("bbbb");
                    dataGridViewX1.Rows.Add(Ca1,TenSP1, MaCT1, TenCT1, CDay1, CRong1, CDai1, SLDat_d, SLTramTret_d, SLLoai_d, GhiChu1);
                    cb_May_input.Enabled = false;
                    prs.formatcolordatagrid(dataGridViewX1);
                    txt_CRong_input.Text = "";
                    txt_CDai_input.Text = "";
                    txt_CDay_input.Text = "";
                    txt_Dat_input.Text = "";
                    txt_Loai_input.Text = "";
                    txt_Tramtret_input.Text = "";
                    cb_MaSP_input.Focus();
                    //reset();
                    

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bt_insert_Click(object sender, EventArgs e)
        {
            Add_grid();
        }
        private void save()
        {
            Data dta = new Data();
            SqlConnection Conn = dta.Connect();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Ins_MayBao";
            cmd.CommandType = CommandType.StoredProcedure;
            Conn.Open();
            cmd.Connection = Conn;

            try
            {

                if (dataGridViewX1.Rows.Count > 1)
                {
                    TenMay1 = cb_May_input.Text.Trim();
                    //Ca1 = cb_Ca_input.Text.Trim();
                    LoaiGo1 = cb_LoaiGo_input.Text.Trim();
                    string[] temp = txt_Ngayinput.Text.Split('/');
                    DateBegin1 = temp[1] + "/" + temp[0] + "/" + temp[2];
                    temp.Clone();
                    i = 0;
                    while (dataGridViewX1.Rows.Count>1)
                    {
                        //MaCT1 = "'Null'";
                        Ca1 = dataGridViewX1.Rows[0].Cells["Ca"].Value.ToString();
                        MaCT1 = dataGridViewX1.Rows[0].Cells["MaCT"].Value.ToString();
                        CDay1 = dataGridViewX1.Rows[0].Cells["ChieuDay"].Value.ToString();
                        CRong1 = dataGridViewX1.Rows[0].Cells["ChieuRong"].Value.ToString();
                        CDai1 = dataGridViewX1.Rows[0].Cells["ChieuDai"].Value.ToString();
                        SLDat1 = dataGridViewX1.Rows[0].Cells["SLDat"].Value.ToString();
                        SLTramTret1 = dataGridViewX1.Rows[0].Cells["SLTramTret"].Value.ToString();
                        SLLoai1 = dataGridViewX1.Rows[0].Cells["SLLoai"].Value.ToString();
                        GhiChu1 = dataGridViewX1.Rows[0].Cells["GhiChu"].Value.ToString();
                       
                        cmd.Parameters.AddWithValue("@TenMay", TenMay1);
                        cmd.Parameters.AddWithValue("@Ca", Ca1);
                        cmd.Parameters.AddWithValue("@LoaiGo", LoaiGo1);
                        cmd.Parameters.AddWithValue("@MaCT", MaCT1);
                        cmd.Parameters.AddWithValue("@CDay", CDay1);
                        cmd.Parameters.AddWithValue("@CRong", CRong1);
                        cmd.Parameters.AddWithValue("@CDai", CDai1);
                        cmd.Parameters.AddWithValue("@SLDat", SLDat1);
                        cmd.Parameters.AddWithValue("@SLTT", SLTramTret1);
                        cmd.Parameters.AddWithValue("@SLLoai", SLLoai1);
                        cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                        cmd.Parameters.AddWithValue("@Ngay", DateBegin1);
                        cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                        dataGridViewX1.Rows.RemoveAt(0);
                       
                    }
                    MessageBox.Show("Dữ Liệu được lưu thành công");
                    reset();
                    cb_May_input.Text = "";
                    cb_May_input.Focus();

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            save();
        }

        private void dataGridViewX1_DoubleClick(object sender, EventArgs e)
        {
            if (dataGridViewX1.Rows.Count > 1)
            {
                try
                {
                    int r = dataGridViewX1.CurrentCell.RowIndex;
                  
                    dataGridViewX1.Rows.RemoveAt(r);
                    //MessageBox.Show("aaaa");
                    i = dataGridViewX1.Rows.Count - 1;
                    dataGridViewX1.CurrentCell = dataGridViewX1.Rows[i].Cells[0];
                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message); }
            }
        }

        private void txt_Dat_input_Leave(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_Dat_input);
        }

        private void txt_Tramtret_input_Leave(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_Tramtret_input);
        }

        private void txt_Loai_input_Leave(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_Loai_input);
        }

        private void FrMayBao_KeyDown(object sender, KeyEventArgs ex)
        {
            if (ex.KeyData == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
            if (ex.KeyData == Keys.F11)
            {
                bt_save_Click(null, null);
            }
            if (ex.KeyData == Keys.PageDown)
            {
                bt_insert_Click(null, null);
            }
        }

        private void bt_reset_Click(object sender, EventArgs e)
        {
            reset();
        }
        private void search()
        {
            SqlConnection Conn = prs.Connect();
            Conn.Open();
            string[] temp = txt_Date_Search.Text.Split('/');
            DateBegin1 = temp[1] + "/" + temp[0] + "/" + temp[2];
            temp.Clone();
            string_temp = "SELECT [Serial],[TenMay],Load_CTSP_Mua.TenSP,[Ca],MayBao.[LoaiGo],[MaCT],Load_CTSP_Mua.TenChiTiet,[CDay],[CRong],[CDai],[SLDat],[SLTT],[SLLoai],[GhiChu],convert(varchar(10),[Ngay],103) as Ngay FROM [dbo].[MayBao] left join dbo.Load_CTSP_Mua on Load_CTSP_Mua.MaChiTiet=dbo.MayBao.MaCT where Ngay>='"+DateBegin1+" 00:00' and Ngay<='"+DateBegin1+" 23:59'";
            DataSet ds = new DataSet();
            try
            {
                if (cb_MaSP_Search.Text != "")
                    string_temp += " and TenMay='"+cb_MaSP_Search.Text+"'";
                
                ds = prs.dts(string_temp);
                if (dataGridViewX2.Rows.Count > 1)
                    dataGridViewX2.Rows.Clear();
                for (i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    
                    MaCT1 = ds.Tables[0].Rows[i]["MaCT"].ToString();
                    CDay1 = ds.Tables[0].Rows[i]["CDay"].ToString();
                    CRong1 = ds.Tables[0].Rows[i]["CRong"].ToString();
                    CDai1 = ds.Tables[0].Rows[i]["CDai"].ToString();
                    SLDat1 = ds.Tables[0].Rows[i]["SLDat"].ToString();
                    SLTramTret1 = ds.Tables[0].Rows[i]["SLTT"].ToString();
                    SLLoai1 = ds.Tables[0].Rows[i]["SLLoai"].ToString();
                    GhiChu1 = ds.Tables[0].Rows[i]["GhiChu"].ToString();
                    TenMay1 = ds.Tables[0].Rows[i]["TenMay"].ToString();
                    LoaiGo1 = ds.Tables[0].Rows[i]["LoaiGo"].ToString();
                    Ca1 = ds.Tables[0].Rows[i]["Ca"].ToString();
                    Serial1 = ds.Tables[0].Rows[i]["Serial"].ToString();
                    TenSP1 = ds.Tables[0].Rows[i]["TenSP"].ToString();
                    TenCT1 = ds.Tables[0].Rows[i]["TenChiTiet"].ToString();
                    DateBegin1=ds.Tables[0].Rows[i]["Ngay"].ToString();
                    dataGridViewX2.Rows.Add(false, Serial1, TenMay1, TenSP1, MaCT1, TenCT1, CDay1, CRong1, CDai1, SLDat1, SLTramTret1, SLLoai1, DateBegin1, GhiChu1, LoaiGo1, Ca1);
                    //dataGridViewX2.Rows.Add(false,Serial1,


                }
                prs.formatcolordatagrid(dataGridViewX2);
                ds.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                
            }
        }
        private void bt_searchoutput_Click(object sender, EventArgs e)
        {
            search();
        }
        private void Update()
        {
            SqlConnection Conn = prs.Connect();
            Conn.Open();
            string check = "";
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Conn;
            try
            {
                DialogResult d = MessageBox.Show("Bạn có chắc muốn sửa những thông tin được check phía dưới", "Thông Báo", MessageBoxButtons.OKCancel);
                if (d == DialogResult.OK)
                {
                    while (dataGridViewX2.Rows.Count > 1)
                    {
                        check = dataGridViewX2.Rows[0].Cells["checkbox"].Value.ToString();
                        //MessageBox.Show(check);
                        if (check == "1" || check == "True")
                        {
                            //MaCT1 = dataGridViewX2.Rows[0].Cells["MaCT_Update"].Value.ToString();
                            CDay1 = dataGridViewX2.Rows[0].Cells["CDay_Update"].Value.ToString();
                            CRong1 = dataGridViewX2.Rows[0].Cells["CRong_Update"].Value.ToString();
                            CDai1 = dataGridViewX2.Rows[0].Cells["CDai_Update"].Value.ToString();
                            SLDat1 = dataGridViewX2.Rows[0].Cells["SLDat_Update"].Value.ToString();
                            SLTramTret1 = dataGridViewX2.Rows[0].Cells["SLTT_Update"].Value.ToString();
                            SLLoai1 = dataGridViewX2.Rows[0].Cells["SLLoai_Update"].Value.ToString();
                            GhiChu1 = dataGridViewX2.Rows[0].Cells["GhiChu_Update"].Value.ToString();
                            TenMay1 = dataGridViewX2.Rows[0].Cells["TenMay_Update"].Value.ToString();
                            LoaiGo1 = dataGridViewX2.Rows[0].Cells["LoaiGo_Update"].Value.ToString();
                            Ca1 = dataGridViewX2.Rows[0].Cells["Ca_Update"].Value.ToString();
                            Serial1 = dataGridViewX2.Rows[0].Cells["Serial_Update"].Value.ToString();
                            string[] temp = dataGridViewX2.Rows[0].Cells["Ngay_Update"].Value.ToString().Split('/');
                            //txt_Date_Search.Text.Split('/');
                            DateBegin1 = temp[1] + "/" + temp[0] + "/" + temp[2];
                            temp.Clone();
                            string_temp = @"UPDATE [dbo].[MayBao]
                               SET [TenMay] = '" + TenMay1 + "',[Ca] =N'" + Ca1 + "' ,[LoaiGo] =N'" + LoaiGo1 + "' ,[MaCT] =N'" + MaCT1 + "',[CDay] =" + CDay1 + " ,[CRong] =" + CRong1 + " ,[CDai] =" + CDai1 + " ,[SLDat] =" + SLDat1 + " ,[SLTT] =" + SLTramTret1 + " ,[SLLoai] =" + SLLoai1 + " ,[GhiChu] = N'" + GhiChu1 + "' ,[Ngay] ='" + DateBegin1 + "'  WHERE Serial=" + Serial1;
                            cmd.CommandText = string_temp;
                            //MessageBox.Show(cmd.CommandText);
                            cmd.ExecuteNonQuery();

                        }
                        dataGridViewX2.Rows.RemoveAt(0);
                        

                    }
                    MessageBox.Show("Dữ liệu được sửa thành công");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }

        }

        private void bt_edit_Click(object sender, EventArgs e)
        {
            Update();
        }

        private void bt_delete_Click(object sender, EventArgs e)
        {
                  SqlConnection Conn = prs.Connect();
            Conn.Open();
            string check = "";
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Conn;
            try
            {
                DialogResult d = MessageBox.Show("Bạn có chắc muốn Xóa những thông tin được check phía dưới", "Thông Báo", MessageBoxButtons.OKCancel);
                if (d == DialogResult.OK)
                {
                    while (dataGridViewX2.Rows.Count > 1)
                    {
                        check = dataGridViewX2.Rows[0].Cells["checkbox"].Value.ToString();
                        //MessageBox.Show(check);
                        if (check == "1" || check == "True")
                        {
                            cmd.CommandText = "DELETE FROM [dbo].[MayBao] WHERE Serial=" + dataGridViewX2.Rows[0].Cells["Serial_Update"].Value.ToString();
                            //MessageBox.Show(cmd.CommandText);
                            cmd.ExecuteNonQuery();

                        }
                        dataGridViewX2.Rows.RemoveAt(0);

                    }
                    MessageBox.Show("Dữ liệu được xóa thành công");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        private void report()
        {
            try
            {
                string[] temp = txt_Date_Begin.Text.Split('/');
                DateBegin1 = temp[1] + "/" + temp[0] + "/" + temp[2];
                temp.Clone();
                string[] temp1 = txt_Date_End.Text.Split('/');
                DateEnd1 = temp1[1] + "/" + temp1[0] + "/" + temp1[2];
                temp1.Clone();
                string_temp = @"select qry1.TenMay,qry1.SLDatHC,isnull(qry2.SLDatTC,0) as SLDatTC,qry1.SLLoaiHC,isnull(qry2.SLLoaiTC,0) as SLLoaiTC,qry1.MToiDatHC,isnull(qry2.MToiDatTC,0) as MToiDatTC,qry1.MToiLoaiHC,isnull(qry2.MToiLoaiTC,0) as MToiLoaiTC,qry1.MKhoiDatHC,isnull(qry2.MToiDatTC,0) as MToiDatTC,qry1.MKhoiLoaiHC,isnull(qry2.MKhoiLoaiTC,0) as MKhoiLoaiTC,qry1.MToiDatHC+isnull(qry2.MToiDatTC,0) as MToiDat,qry1.MToiLoaiHC+isnull(qry2.MToiLoaiTC,0) as MToiLoai,qry1.MKhoiDatHC+isnull(qry2.MKhoiDatTC,0) as MKhoiDat,qry1.MKhoiLoaiHC+isnull(qry2.MKhoiLoaiTC,0) as MKhoiLoai,(qry1.MKhoiLoaiHC+isnull(qry2.MKhoiLoaiTC,0))/(qry1.MKhoiLoaiHC+isnull(qry2.MKhoiLoaiTC,0)+qry1.MKhoiDatHC+isnull(qry2.MKhoiDatTC,0)) as TLLoai  from
                        (SELECT 
                              [TenMay]      
                              ,sum([SLDat]+[SLTT]) as SLDatHC
                              ,sum([SLLoai]) as SLLoaiHC
                              ,sum(([SLDat]+[SLTT])*CDai)/1000 as MToiDatHC
                              ,SUM(SLLoai*CDai)/1000 as MToiLoaiHC
                              ,SUM(([SLDat]+[SLTT])*CDai*CRong*CDay)/1000000000 as MKhoiDatHC
                              ,SUM(SLLoai*CDai*CRong*CDay)/1000000000 as MKhoiLoaiHC
                          FROM [dbo].[MayBao]
                          where Ca ='HC' and Ngay>='" + DateBegin1 + " 00:00' and Ngay<='" + DateEnd1 + " 23:59' group by TenMay) as qry1";
                string_temp += @"               left outer join (SELECT 
                              [TenMay]
     
      
                              ,sum([SLDat]+[SLTT]) as SLDatTC
                              ,sum([SLLoai]) as SLLoaiTC
                              ,sum(([SLDat]+[SLTT])*CDai)/1000 as MToiDatTC
                              ,SUM(SLLoai*CDai)/1000 as MToiLoaiTC
                              ,SUM(([SLDat]+[SLTT])*CDai*CRong*CDay)/1000000000 as MKhoiDatTC
                              ,SUM(SLLoai*CDai*CRong*CDay)/1000000000 as MKhoiLoaiTC
       
                          FROM [dbo].[MayBao]
                          where Ca ='TC' and Ngay>='" + DateBegin1 + " 00:00' and Ngay<='" + DateEnd1 + " 23:59' group by TenMay) as qry2 on qry1.TenMay=qry2.TenMay";
                DataSet ds = new DataSet();
                ds = prs.dts(string_temp);
                dataGridViewX3.DataSource = ds.Tables[0];
                prs.formatcolordatagrid(dataGridViewX3);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void bt_Search_report_Click(object sender, EventArgs e)
        {
            report();
        }

        private void bt_ExportExcel_Click(object sender, EventArgs e)
        {
            if (dataGridViewX3.Rows.Count > 1)
            {

                DataTable dt = (DataTable)dataGridViewX3.DataSource;
                    prs.ExportExcel(dt, "C:\\Huynh\\MayBao.xlsx", 6, 1);
                    // prs.ExportExcel(dt, @"\\192.168.1.10\SHARE\Book1.xlsx", 6, 1);
               
                
            }
            else
                MessageBox.Show("Không có dữ liệu để xuất ra excel");
        }
        private void BangKe()
        {
            
            string[] temp = dateTimePicker1.Text.Split('/');
            DateBegin1 = temp[1] + "/" + temp[0] + "/" + temp[2];
            temp.Clone();
            string[] temp1 = dateTimePicker2.Text.Split('/');
            DateEnd1 = temp1[1] + "/" + temp1[0] + "/" + temp1[2];
            temp1.Clone();
            string_temp = @"SELECT [Serial],[TenMay],Load_CTSP_Mua.TenSP,[MaCT],Load_CTSP_Mua.TenChiTiet,[CDay],[CRong],[CDai],[SLDat],[SLTT],[SLLoai],[Ngay],[Ca],MayBao.[LoaiGo],[GhiChu],NgayInsert FROM [dbo].[MayBao] left join dbo.Load_CTSP_Mua on Load_CTSP_Mua.MaChiTiet=dbo.MayBao.MaCT where NgayInsert>='" + DateBegin1 + " 00:00' and NgayInsert<='" + DateEnd1 + " 23:59'";
            DataSet ds = new DataSet();
            try
            {
                ds = prs.dts(string_temp);
                dataGridViewX4.DataSource = ds.Tables[0];
                prs.formatcolordatagrid(dataGridViewX4);
                //MessageBox.Show(dataGridViewX4.Rows.Count.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bt_serach_BangKe_Click(object sender, EventArgs e)
        {
            BangKe();
        }

        private void bt_BangKe_Excel_Click(object sender, EventArgs e)
        {
            if (dataGridViewX4.Rows.Count > 1)
            {

                DataTable dt = (DataTable)dataGridViewX4.DataSource;
                prs.ExportExcel(dt, "C:\\Huynh\\MayBao_BK.xlsx", 6, 1);
                // prs.ExportExcel(dt, @"\\192.168.1.10\SHARE\Book1.xlsx", 6, 1);


            }
            else
                MessageBox.Show("Không có dữ liệu để xuất ra excel");
        }

        private void cb_Ca_input_Leave(object sender, EventArgs e)
        {
            if (cb_Ca_input.Text != "HC" && cb_Ca_input.Text != "TC")
            {
                MessageBox.Show("Nhập sai thông tin");
                cb_Ca_input.Text = "";
            }
        }
    }
}
